package com.xiaolv.web.controller;

import com.xiaolv.web.service.SysOperateTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 初使化数据表
 */
@RequestMapping("/init")
@RestController
public class InitController {

    @Autowired
    private SysOperateTableService operateTableService;

    /**
     * 创建数据表
     *
     * @return
     */
    @GetMapping("/createTable")
    public Boolean createLogTable() {

        //需要配置application-test.yml default-data-source-name: ds_history
//        //创建日志表
//        for (int i = 2020; i <= 2021; i++) {
//            for (int j = 1; j <= 12; j++) {
//                for (int k = 1; k <= 31; k++) {
//                    //数据表名
//                    String tableName = "sys_log_" + getNumber(i) + getNumber(j) + getNumber(k);
//                    //判断表是否存在
//                    int status = operateTableService.existTable(tableName);
//                    if (status == 0) {
//                        System.out.println(tableName);
//                        operateTableService.createLogTable(tableName);
//                    } else {
//                        operateTableService.dropTable(tableName);
//                        operateTableService.createLogTable(tableName);
//                    }
//                }
//            }
//        }
        //需要配置application-test.yml default-data-source-name: ds_master_slave
        //创建订单表
        for (int i = 2020; i <= 2021; i++) {
            for (int j = 1; j <= 12; j++) {
                //数据表名
                String tableName = "t_order_" + getNumber(i) + getNumber(j);
                //判断表是否存在
                int status = operateTableService.existTable(tableName);
                if (status == 0) {
                    System.out.println(tableName);
                    operateTableService.createOrderTable(tableName);
                } else {
                    operateTableService.dropTable(tableName);
                    operateTableService.createOrderTable(tableName);
                }
            }
        }
        return true;
    }

    /**
     * 得到加0数字
     *
     * @param n
     * @return
     */
    public String getNumber(int n) {
        if (n < 10) {
            return "0" + n;
        }
        return String.valueOf(n);
    }
}
